package com.sxbbc.common.core.service.impl;

import cn.jpush.api.push.PushResult;
import com.sxbbc.common.basics.push.AppPushUtil;
import com.sxbbc.common.basics.push.PushVo;
import com.sxbbc.common.basics.service.config.Config;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.dto.Properties;
import com.sxbbc.common.basics.utils.properties.PropertiesUtil;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CPushLog;
import com.sxbbc.common.core.enums.PushTypeEnums;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.ICPushLogService;
import com.sxbbc.common.core.service.IPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息推送 业务实现类
 *
 * @author leehao
 * @date 2019-06-17
 */
@Service
public class PushServiceImpl implements IPushService {

    @Autowired
    private ICPushLogService cPushLogService;
    @Autowired
    private ICCustomerService customerService;

    @Override
    public void pushMessageById(PushVo pushVo) {
        try {
        	CCustomer customer = customerService.getById(pushVo.getCid());
        	if (customer != null) {
        		pushVo.setToken(customer.getInviteCode());
        	}
            Properties properties = Config.properties;
            String remark = "";
            if (pushVo.getParams() != null && pushVo.getParams().length > 0) {
                remark = PropertiesUtil.getInstance().getVal(pushVo.getContent(),pushVo.getParams());
            }else{
                remark = PropertiesUtil.getInstance().getVal(pushVo.getContent());
            }
            PushResult result = AppPushUtil.getInstance().pushAndroidAndIos(properties.getJpushKey(),properties.getJpushSecret(),pushVo.getToken(),remark);
            if (CalcuUtils.getInstance().isEquals(0,result.statusCode)) {
                CPushLog pushLog = new CPushLog();
                pushLog.setCid(pushVo.getCid());
                pushLog.setContent(pushVo.getContent());
                pushLog.setRemark(remark);
                if (pushVo.getParams() != null && pushVo.getParams().length > 0) {
                    StringBuffer str = new StringBuffer();
                    for (String param : pushVo.getParams()) {
                        str.append(param).append(",");
                    }
                    pushLog.setParams(str.toString().substring(0,str.toString().length() - 1));
                }
                pushLog.setType(pushVo.getType());
                pushLog.setPayCode(String.valueOf(result.sendno));
                pushLog.setTargetCode(pushVo.getTargetCode());
                cPushLogService.save(pushLog);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 推送平台消息
     * @param pushType
     * @param cid
     * @param content
     */
    @Override
    public void pushPlatMessage(String pushType, Integer cid,String content) {
        try {
            Properties properties = Config.properties;
            String token = null;
            PushResult result = null;
            if(pushType.equals("single")){
                CCustomer customer = customerService.getById(cid);
                if (customer != null) {
                    token = customer.getInviteCode();
                }
                result = AppPushUtil.getInstance().pushAndroidAndIos(properties.getJpushKey(),properties.getJpushSecret(),token,content);
                if (CalcuUtils.getInstance().isEquals(0,result.statusCode)) {
                    CPushLog pushLog = new CPushLog();
                    pushLog.setCid(cid);
                    pushLog.setContent(content);
                    pushLog.setType(PushTypeEnums.PLAT_NOTICE.getId());
                    pushLog.setPayCode(String.valueOf(result.sendno));
                    pushLog.setTargetCode(String.valueOf(cid));
                    cPushLogService.save(pushLog);
                }
            }else if(pushType.equals("all")){
                AppPushUtil.getInstance().pushAllAndroidAndIos(properties.getJpushKey(),properties.getJpushSecret(),content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
